package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.service.TeacharService;

import com.itheima.edu.info.manager.domain.Teachar;

import java.util.Scanner;

public class TeacharController {
    private Scanner sc = new Scanner(System.in);
    private TeacharService teacharService = new TeacharService();

    public void start() {
        teacharLoop:
        while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
//                    System.out.println("添加老师");
                    addTeachar();
                    break;
                case 2:
//                    System.out.println("删除老师");
                    deleteTeacherById();
                    break;
                case 3:
//                    System.out.println("修改老师");
                    updeteTeacher();
                    break;
                case 4:
//                    System.out.println("查询老师");
                    findAllTeacher();
                    break;
                case 5:
                    System.out.println("感谢您使用老师管理系统，再见！");
                    break teacharLoop;
                default:
                    System.out.println("您的输入有误，请重新输入");
            }
        }

    }

    public void updeteTeacher() {
        //调用添加id和判断id方法
        String id=inputTeacherId();

        Teachar newteacher=inputTecherInfo(id);


        teacharService.updateTeacher(id,newteacher);
        System.out.println("修改成功");
    }

    public void deleteTeacherById() {
        //调用添加id方法，并进行赋值
        String id=inputTeacherId();

        teacharService.deleteTeacherById(id);
        System.out.println("删除成功");


    }

    public void findAllTeacher() {
        //从业务员中获取数组
        Teachar[] teachar = teacharService.findAllTeacher();
        if (teachar == null) {
            System.out.println("查无信息，请输入后重试");
            return;
        }
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < teachar.length; i++) {
            Teachar t= teachar[i];
            if(t!=null){
                System.out.println(t.getId()+"\t"+t.getName()+"\t"+ t.getAge()+"\t\t"+t.getBirthday());
            }
        }
    }

    public void addTeachar() {
        String id;
        while (true) {
            //1.接收不存在的老师
            System.out.println("请输入老师");
            id = sc.next();

            //2.判断id是否存在- --判断就是业务
            boolean exists = teacharService.isexits(id);

            if (exists) {//true存在就重新输入
                System.out.println("id已经存在，请重新输入");
            } else break;

        }

        //3.接收老师的其他信息
        Teachar t=inputTecherInfo(id);

        boolean result = teacharService.addTeacher(t);
        if (result) {
            System.out.println("添加成功");
        } else System.out.println("添加失败");
    }

    public String inputTeacherId(){
        //增加作用域
        String id;

        //2.不明确要输入几次，用while无限循环
        while (true) {
            System.out.println("请输入id");
            id= sc.next();

            //1.判断id是否存在
            boolean esists= teacharService.isexits(id);
            if (!esists) {
                System.out.println("请重新输入，id不存在");
            }else break;
        }
        return id;
    }

    public Teachar   inputTecherInfo(String id){
        System.out.println("请输入老师的姓名");
        String name = sc.next();
        System.out.println("请输入老师的年龄");
        int age = sc.nextInt();
        System.out.println("请输入老师的生日");
        String birthday = sc.next();

        //2.键盘录入新的老师信息
        Teachar newteacher = new Teachar();
        newteacher.setAge(age);
        newteacher.setBirthday(birthday);
        newteacher.setName(name);
        newteacher.setId(id);

        return newteacher;
    }
}
